package com.dao.impl;

import com.dao.ShopCartProductDao;
import com.pojo.ShopCartProduct;
import com.utils.JDBCUtil;

import java.util.List;

public class ShopCartProductDaoImpl implements ShopCartProductDao {

    /**
     * 为购物车商品表添加数据
     * @param shopCartProduct
     * @return
     */
    @Override
    public int addSP(ShopCartProduct shopCartProduct) {
        return JDBCUtil.update("insert into s_shop_cart_product values(?,?,?,?)",shopCartProduct.getId(),shopCartProduct.getShopCartId(),shopCartProduct.getProductId(),shopCartProduct.getProductNum());
    }

    /**
     * 根据购物车ID查找该购物车内的商品
     * @param sCID
     * @return
     */
    @Override
    public List<ShopCartProduct> selectByShopCartID(String sCID) {
        return JDBCUtil.queryList(ShopCartProduct.class,"select * from s_shop_cart_product where shop_cart_id = ?",sCID);
    }

    /**
     * 修改购物车商品
     * @param shopCartProduct
     * @return
     */
    @Override
    public int updateSC(ShopCartProduct shopCartProduct) {
        return JDBCUtil.update("update s_shop_cart_product set product_num = (product_num+?) where id = ?",shopCartProduct.getProductNum(),shopCartProduct.getId());
    }

    /**
     * 根据id删除购物车商品
     * @param id
     * @return
     */
    @Override
    public int deleteSC(String id) {
        return JDBCUtil.update("delete from s_shop_cart_product where id =?",id);
    }

    /**
     * 根据购物车id和商品id查找数据
     * @param ShopCartID
     * @param productID
     * @return
     */
    @Override
    public ShopCartProduct selectByShopCartIDAndProductID(String ShopCartID, String productID) {
        return JDBCUtil.queryOne(ShopCartProduct.class,"select * from s_shop_cart_product where shop_cart_id = ? and product_id = ?",ShopCartID,productID);
    }


}
